using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Rapier.Kodachi.Core
{
    public partial class Settings : Form
    {
        #region Constructor
        public Settings()
        {
            InitializeComponent();
            SetupBinding();
        } 
        #endregion

        #region Private Methods
        private void SetupBinding()
        {
            listFolders.DataSource = CorePlugin.CoreSettings.IndexFolders;
            listTextExtensions.DataSource = CorePlugin.CoreSettings.TextFileExtensions;
        }

        private void EditSelectedFolder()
        {
            AddEditIndexFolder aeif = new AddEditIndexFolder(CorePlugin.CoreSettings.IndexFolders[listFolders.SelectedIndex]);
            aeif.ShowDialog();
        }
        #endregion

        #region Event Handlers
		private void buttonClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonAddFolder_Click(object sender, EventArgs e)
        {
            AddEditIndexFolder aeif = new AddEditIndexFolder();
            if (aeif.ShowDialog() == DialogResult.Cancel)
                return;

            if (CorePlugin.CoreSettings.IndexFolders.Contains(aeif.IndexFolder))
            {
                MessageBox.Show("This folder is already being indexed. It will not be added to the list.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            CorePlugin.CoreSettings.IndexFolders.Add(aeif.IndexFolder);
        }

        private void buttonEditFolder_Click(object sender, EventArgs e)
        {
            if (listFolders.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a folder from the list on the left first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            EditSelectedFolder();
        }

        private void buttonRemoveFolder_Click(object sender, EventArgs e)
        {
            if (listFolders.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a folder from the list on the left first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            CorePlugin.CoreSettings.IndexFolders.RemoveAt(listFolders.SelectedIndex);
        }

        private void buttonAddExtension_Click(object sender, EventArgs e)
        {
            GetTextExtension gte = new GetTextExtension();
            if (gte.ShowDialog() == DialogResult.Cancel)
                return;

            CorePlugin.CoreSettings.TextFileExtensions.Add(gte.Extension);
        }

        private void buttonRemoveExtension_Click(object sender, EventArgs e)
        {
            if (listTextExtensions.SelectedIndex == -1)
            {
                MessageBox.Show("Please select an extension from the list on the left first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            CorePlugin.CoreSettings.TextFileExtensions.RemoveAt(listTextExtensions.SelectedIndex);
        }

        private void listFolders_DoubleClick(object sender, EventArgs e)
        {
            EditSelectedFolder();
        }
        #endregion
    }
}